﻿package com.kaishengit.web;

import java.io.IOException;

//import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSession;

public class HelloServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public HelloServlet(){
		//System.out.println("实例化!");
	}
	@Override
	public void init() throws ServletException {
		//String name = getServletConfig().getInitParameter("name");
		//String password = getServletConfig().getInitParameter("password");
		//System.out.println("name:"+name+"\tpassword:"+password);
		//System.out.println("初始化！");
	}
	/*@Override
	public void init(ServletConfig config) throws ServletException {
		String name = config.getInitParameter("name");
		String password = config.getInitParameter("password");
		System.out.println("name:"+name+"\tpassword:"+password);
	}*/
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//..............代码段...........
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		if("admin".equals(username) && "123".equals(password)){
			HttpSession session = request.getSession();
			session.setAttribute("currUser", username);
			response.sendRedirect("main.jsp");
		}else{
			response.sendRedirect("index.jsp");
		}
		/*if("admin".equals(username) && "123".equals(password)) {
			HttpSession session = request.getSession();
			session.setAttribute("currUser", username);
			response.sendRedirect("main.jsp");
		} else {
			response.sendRedirect("index.jsp");
		}*/
		/*
		//获取客户端请求服务器的路径
		System.out.println("requestURI:" + request.getRequestURI());
		
		System.out.println("requestURL:" + request.getRequestURL());
		
		//获取当前的协议http
		System.out.println("scheme:" + request.getScheme());
		//服务器名称(域名)
		System.out.println("serverName:" + request.getServerName());
		
		//服务器的端口号
		System.out.println("ServerPost:" + request.getServerPort());
		//获取访问者的IP地址
		System.out.println("RemoteAddr:" + request.getRemoteAddr());
		
		
		//session方式
		HttpSession session = request.getSession(false);
		System.out.println(session);
		*/
		
	}
	
	@Override
	public void destroy() {
		//System.out.println("销毁！");
		super.destroy();
	}
}
